package com.pig4cloud.pig.module.controller.promoter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.admin.api.entity.SysUser;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.common.security.service.PigUser;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.module.api.entity.promoter.PromoterGroup;
import com.pig4cloud.pig.module.service.promoter.IPromoterGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 推广员分组Controller
 *
 * @author ruoyi
 * @date 2022-10-28
 */
@Validated
@Api(value = "推广员分组控制器", tags = {"推广员分组管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/group")
public class PromoterGroupController {

    private final IPromoterGroupService promoterGroupService;
//    private final RemoteUserService remoteUserService;
    /**
     * 查询推广员分组列表
     */
    @ApiOperation("查询推广员分组列表")
    @PreAuthorize("@pms.hasPermission('module:promoterGroup:list')")
    @GetMapping("/list")
    public TableDataInfo<PromoterGroup> list(@Validated PromoterGroup promoterGroup) {
		QueryWrapper<PromoterGroup> qw = new QueryWrapper<>();
		PigUser sysUser = SecurityUtils.getUser();
		if(sysUser.getUserType() == 5){
			qw.eq("sys_user_id",sysUser.getId());
		}
		TableDataInfo<PromoterGroup> td = PageUtils.buildDataInfo(promoterGroupService.page(PageUtils.buildPage(),qw));
		List<PromoterGroup> list = td.getData();
		for (PromoterGroup list1:list){
//			SysUser sysUser1 = sysUserService.getById(list1.getSysUserId());
//			if(sysUser1 != null) {
//				list1.setSysNickName(sysUser1.getNickName());
//			}
		}
		return td;

    }

    /**
     * 导出推广员分组列表
     */
    @ApiOperation("导出推广员分组列表")
    @PreAuthorize("@pms.hasPermission('module:promoterGroup:export')")
    @SysLog("推广员分组")
    @GetMapping("/export")
    public List export(@Validated PromoterGroup promoterGroup) {
        List<PromoterGroup> list = promoterGroupService.list();
        // ExcelUtil<PromoterGroup> util = new // ExcelUtil<PromoterGroup>(PromoterGroup.class);
        return  list;// util.exportExcel(list, "推广员分组");
    }

    /**
     * 获取推广员分组详细信息
     */
    @ApiOperation("获取推广员分组详细信息")
    @PreAuthorize("@pms.hasPermission('module:promoterGroup:query')")
    @GetMapping("/{id}")
    public R<PromoterGroup> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        PromoterGroup promoterGroup = promoterGroupService.getById(id);
        return R.ok(promoterGroup);
    }

    /**
     * 新增推广员分组
     */
    @ApiOperation("新增推广员分组")
    @PreAuthorize("@pms.hasPermission('module:promoterGroup:add')")
    @SysLog("推广员分组")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody PromoterGroup promoterGroup) {
        return R.okByRow(promoterGroupService.save(promoterGroup) ? 1 : 0);
    }

    /**
     * 修改推广员分组
     */
    @ApiOperation("修改推广员分组")
    @PreAuthorize("@pms.hasPermission('module:promoterGroup:edit')")
    @SysLog("推广员分组")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody PromoterGroup promoterGroup) {
        return R.okByRow(promoterGroupService.updateById(promoterGroup) ? 1 : 0);
    }

    /**
     * 删除推广员分组
     */
    @ApiOperation("删除推广员分组")
    @PreAuthorize("@pms.hasPermission('module:promoterGroup:remove')")
    @SysLog("推广员分组")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(promoterGroupService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
